﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="" style="width:80%;margin:0 auto;">
    <div class="layui-form-item">
        <label class="layui-form-label">仓库代码</label>
        <div class="layui-input-block">
            <input type="text" name="RepositoryCode" lay-verify="required" placeholder="请输入仓库代码" autocomplete="off" style="width:300px" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">仓库名称</label>
        <div class="layui-input-block">
            <input type="text" name="RepositoryName" lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" style="width:300px" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">仓库属性</label>
        <div class="layui-input-block">
            <input type="radio" name="RepositoryAttribute" value="数码产品" title="数码产品">
            <input type="radio" name="RepositoryAttribute" value="日用" title="日用">
            <input type="radio" name="RepositoryAttribute" value="电缆" title="电缆">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
        </div>
    </div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui/layui.js"></script>

<script>

    //关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); 
        parent.layer.close(index); 
    }

    layui.use(['form', 'laydate', 'util'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;

        laydate.render({
            elem: '#ID-laydate-demo'
        });

        // 提交事件
        form.on('submit(add)', function (data) {
            var field = data.field; 

            console.log(field);
            $.ajax({
                url: '/Repository/AddRepository', 
                type: 'POST', 
                data: field, 
                dataType: 'json', 
                success: function (res) {
                    if(res == -1)
                    {
                        layer.msg('编号已存在！请重新输入！', {
                            time: 1000,
                            icon: 5
                        });
                    }
                    else if(res == -2)
                    {
                        layer.msg('名称已存在！请重新输入！', {
                            time: 1000,
                            icon: 5
                        });
                    }
                    else if (res == 0) { 
                        layer.alert('添加成功', { title: '操作结果' }, function () {
                            layer.closeAll(); 
                            closeWorkstation()
                        });
                    } else {
                        layer.alert('添加失败：' + res.msg, { title: '操作结果' });
                    }
                },
                error: function (err) {
                    // 处理添加失败的情况
                    layer.alert('添加失败：网络异常，请稍后重试', { title: '操作结果' });
                    console.error('添加失败', err);
                }
            });

            return false; // 阻止默认 form 提交
        });
    });
</script>